package com.hao.tool.web.module.navigation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hao.tool.web.module.navigation.entity.NoticeInfo;
import com.hao.tool.web.module.navigation.mapper.NoticeMapper;
import com.hao.tool.web.module.navigation.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-22 22:16
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, NoticeInfo> implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public boolean saveNotice(NoticeInfo info) {
        info.setId(IdWorker.getIdStr());
        info.setCreateTime(LocalDateTime.now());
        info.setClickCount(0L);
        info.setTitle("title");
        info.setSubTitle("subTitle");
        return this.saveOrUpdate(info);
    }

    @Override
    public String getLastNotice() {
        return noticeMapper.getLastNotice();
    }

    @Override
    public List<NoticeInfo> getLastNotice(Integer num) {
        QueryWrapper<NoticeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("CREATE_TIME");
        queryWrapper.eq("STATUS", 0);
        IPage<NoticeInfo> page = new Page<>();
        page.setCurrent(1);
        page.setSize(num);
        IPage<NoticeInfo> infoIPage = this.page(page, queryWrapper);
        return infoIPage.getRecords();
    }

    @Override
    public NoticeInfo getMain() {
        return this.getById(0l);
    }
}
